﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using EHSubmit.Model;
using EHSubmit.Util;


namespace EHSubmit.Logic
{
    /// <summary>
    ///DepartmentManager 的摘要说明
    /// </summary>
    public class DepartmentManager
    {
        private const string SELECT_DEPARTMENT_ByInstituteID = "select * from Department where Department.InstituteID = @InstituteID";
        private const string SELECT_DEPARTMENT_ByInstituteName = "select Department.DepartmentID,Department.DepartmentName,Department.InstituteID from Department,Institute where Department.InstituteID = Institute.InstituteID and Institute.InstituteName = @InstituteName";
        private const string PARM_InstituteID = "@InstituteID";
        private const string PARM_InstituteName = "@InstituteName";

        public DepartmentManager()
        {

        }

        /// <summary>
        /// 根据学院ID获取专业信息
        /// </summary>
        /// <param name="instituteID">学院ID</param>
        /// <returns>专业Department集合</returns>
        public IList<Department> GetDepartmentByInstituteID(int instituteID)
        {
            try
            {
                IList<Department> list = new List<Department>();
                SqlParameter parm = new SqlParameter(PARM_InstituteID, SqlDbType.Int);
                parm.Value = Convert.ToInt32(instituteID);
                using (SqlDataReader rdrDepartment = (SqlDataReader)SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SELECT_DEPARTMENT_ByInstituteID, parm))
                {
                    while(rdrDepartment.Read())
                    {
                        Department department = new Department();
                        department.DepartmentID = (int)rdrDepartment["DepartmentID"];
                        department.DepartmentName = (string)rdrDepartment["DepartmentName"];
                        department.InstituteID = (int)rdrDepartment["InstituteID"];
                        list.Add(department);
                    }
                };
                return list;

            }
            catch (Exception ex)
            {
                LogHelper.Log(ex.Message, ex);
                return null;
            }
        }

        
        /// <summary>
        /// 根据学院名称获取专业信息
        /// </summary>
        /// <param name="instituteID">学院名称</param>
        /// <returns>专业Department集合</returns>
        public IList<Department> GetDepartmentByInstituteName(string instituteName)
        { 
            try
            {
                IList<Department> list = new List<Department>();
                SqlParameter parm = new SqlParameter(PARM_InstituteName, SqlDbType.NVarChar);
                parm.Value = Convert.ToString(instituteName);
                using (SqlDataReader rdrDepartment = (SqlDataReader)SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SELECT_DEPARTMENT_ByInstituteName, parm))
                {
                    while(rdrDepartment.Read())
                    {
                        Department department = new Department();
                        department.DepartmentID = (int)rdrDepartment["DepartmentID"];
                        department.DepartmentName = (string)rdrDepartment["DepartmentName"];
                        department.InstituteID = (int)rdrDepartment["InstituteID"];
                        list.Add(department);
                    }
                };
                return list;

            }
            catch (Exception ex)
            {
                LogHelper.Log(ex.Message, ex);
                return null;
            }
        }
    }
}

